<%--
  Created by IntelliJ IDEA.
  User: peipei
  Date: 2022/9/3
  Time: 11:14
  To change this template use File | Settings | File Templates.
--%>
<!DOCTYPE html>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html lang="en">
<head>
    <title>Title</title>
    <%@include file="../../views/common.jsp" %>
    <!-- 引入 layui.css -->
    <link href="//unpkg.com/layui@2.7.6/dist/css/layui.css" rel="stylesheet">
    <!-- 引入 layui.js -->
    <script src="//unpkg.com/layui@2.7.6/dist/layui.js"></script>
</head>
<body>
<%--头部工具栏--%>
<script type="text/html" id="headDemo">
    <a class="layui-btn layui-icon layui-icon-add-circle" lay-event="add">添加</a>
    <a class="layui-btn layui-btn-danger layui-icon layui-icon-delete " lay-event="del">删除</a>
</script>
<%--行内工具栏--%>
<script type="text/html" id="rowBarDemo">
    <a class="layui-btn layui-btn-xs" lay-event="update">修改</a>
    <a class="layui-btn layui-btn-danger  layui-btn-xs" lay-event="del">删除</a>
</script>
<%--修改表单--%>
<form class="layui-form" action="" lay-filter="updateFormFilter" id="updateQuestionForm"
      style="display: none;margin-right: 40px;margin-top: 20px">
    <input type="hidden" name="id" id="id">
    <div class="layui-form-item">
        <label class="layui-form-label">问题描述</label>
        <div class="layui-input-block">
            <input type="text" name="question" required lay-verify="required" placeholder="请输入问题描述" autocomplete="off"
                   class="layui-input" id="questionUp"><span style="color: red"></span>
        </div>
    </div>
    <div class="layui-form-item layui-form-text">
        <label class="layui-form-label">选项A</label>
        <div class="layui-input-block">
            <textarea name="optionA" placeholder="请输入内容" required lay-verify="required"
                      class="layui-textarea"></textarea>
        </div>
    </div>
    <div class="layui-form-item layui-form-text">
        <label class="layui-form-label">选项B</label>
        <div class="layui-input-block">
            <textarea name="optionB" placeholder="请输入内容" required lay-verify="required"
                      class="layui-textarea"></textarea>
        </div>
    </div>
    <div class="layui-form-item layui-form-text">
        <label class="layui-form-label">选项C</label>
        <div class="layui-input-block">
            <textarea name="optionC" placeholder="请输入内容" required lay-verify="required"
                      class="layui-textarea"></textarea>
        </div>
    </div>
    <div class="layui-form-item layui-form-text">
        <label class="layui-form-label">选项D</label>
        <div class="layui-input-block">
            <textarea name="optionD" placeholder="请输入内容" required lay-verify="required"
                      class="layui-textarea"></textarea>
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">问题类型</label>
        <div class="layui-input-block">
            <select name="type" lay-verify="required">
                <option value="1">正向计分</option>
                <option value="2">反向计分</option>
            </select>
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">状态</label>
        <div class="layui-input-block">
            <input id="status" type="checkbox" name="status" value="0" checked lay-skin="switch" lay-text="可用|失效">
        </div>
    </div>
    <div class="layui-form-item">
        <div class="layui-input-block">
            <button class="layui-btn" lay-submit lay-filter="updateFormFilter" id="upQuestion">立即提交</button>
            <button type="reset" class="layui-btn layui-btn-primary">重置</button>
        </div>
    </div>
</form>
<%--添加表单--%>
<form class="layui-form" action="" id="saveQuestionForm" style="display: none;margin-right: 40px;margin-top: 20px">
    <div class="layui-form-item">
        <label class="layui-form-label">问题描述</label>
        <div class="layui-input-block">
            <input type="text" name="question" required lay-verify="required" placeholder="请输入问题描述" autocomplete="off"
                   class="layui-input" id="questionAdd"><span style="color: red"></span>
        </div>
    </div>
    <div class="layui-form-item layui-form-text">
        <label class="layui-form-label">选项A</label>
        <div class="layui-input-block">
            <textarea name="optionA" placeholder="请输入内容" required lay-verify="required"
                      class="layui-textarea"></textarea>
        </div>
    </div>
    <div class="layui-form-item layui-form-text">
        <label class="layui-form-label">选项B</label>
        <div class="layui-input-block">
            <textarea name="optionB" placeholder="请输入内容" required lay-verify="required"
                      class="layui-textarea"></textarea>
        </div>
    </div>
    <div class="layui-form-item layui-form-text">
        <label class="layui-form-label">选项C</label>
        <div class="layui-input-block">
            <textarea name="optionC" placeholder="请输入内容" required lay-verify="required"
                      class="layui-textarea"></textarea>
        </div>
    </div>
    <div class="layui-form-item layui-form-text">
        <label class="layui-form-label">选项D</label>
        <div class="layui-input-block">
            <textarea name="optionD" placeholder="请输入内容" required lay-verify="required"
                      class="layui-textarea"></textarea>
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">问题类型</label>
        <div class="layui-input-block">
            <select name="type" lay-verify="required">
                <option value="1">正向计分</option>
                <option value="2">反向计分</option>
            </select>
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">状态</label>
        <div class="layui-input-block">
            <input type="checkbox" name="status" value="0" checked lay-skin="switch" lay-text="开启|关闭">
        </div>
    </div>
    <div class="layui-form-item">
        <div class="layui-input-block">
            <button class="layui-btn" lay-submit lay-filter="saveQuestionFilter" id="addQuestion">立即提交</button>
            <button type="reset" class="layui-btn layui-btn-primary">重置</button>
        </div>
    </div>
</form>
<%--模糊查询--%>
<form class="layui-form" action="" style="margin-top: 10px">
    <div class="layui-inline">
        <label class="layui-form-label">问题</label>
        <div class="layui-input-inline">
            <input type="text" name="question" placeholder="请输入问题" autocomplete="off" class="layui-input">
        </div>
    </div>
    <div class="layui-inline">
        <label class="layui-form-label">创建人</label>
        <div class="layui-input-inline">
            <input type="text" name="createBy" placeholder="请输入创建人" autocomplete="off" class="layui-input">
        </div>
    </div>
    <div class="layui-inline">
        <div class="layui-input-inline">
            <button class="layui-btn" lay-submit lay-filter="selectFormfilter">查询</button>
            <button type="reset" class="layui-btn layui-btn-primary" id="myButton">重置</button>
        </div>
    </div>
</form>
<table id="demo" lay-filter="test"></table>
</body>
<script>


    layui.use(['table', 'form', 'jquery', 'upload'], function () {
        var table = layui.table;
        var form = layui.form;
        var $ = layui.jquery;
        var upload = layui.upload;

        //执行实例
        var uploadInst = upload.render({
            elem: '#test1' //绑定元素
            , url: 'upload.do' //上传接口
            , done: function (res) {
                console.log(res.data)
                $("#avatarsrc").attr('src', res.data)
                $("#avatarsrc").attr('style', "width: 100px;height:80px;margin-right: 40px;display:black")

                form.val("saveQuestionForm", {
                    avatar: res.data,
                })
                //上传完毕回调
                form.render()
            }
            , error: function () {
                //请求异常回调
            }
        });

        $(document).on('click', '.layui-upload-img', function (data) {
            var imgSrc = $(this).attr('src');
            layer.open({
                type: 1
                , title: false
                , closeBtn: 1
                , skin: 'layui-layer-nobg'
                , shadeClose: false
                , content: '<img style="width:100%;height:100%;" class="layui-upload-img" src="' + imgSrc + '"/>'
                , scrollbar: false
            })
        })
        //第一个实例
        table.render({
            elem: '#demo'
            , height: 440
            , url: '${pageContext.request.contextPath}/questionSas/questionSasAll.do' //数据接口
            , page: true
            , limit: 5
            , limits: [5, 10, 15, 20, 25, 30, 100]
            , toolbar: '#headDemo'
            //关闭前端的自动排序，
            , autoSort: false
            , cols: [[ //表头
                {type: 'checkbox', fixed: 'left'}
                , {field: 'id', title: 'ID', width: 70, sort: true, fixed: 'left'}
                , {field: 'question', title: '问题', width: 130}
                , {field: 'optionA', title: '选项A', width: 130}
                , {field: 'optionB', title: '选项B', width: 130}
                , {field: 'optionC', title: '选项C', width: 130}
                , {field: 'optionD', title: '选项D', width: 130}
                , {
                    field: 'type', title: '类型', width: 130,
                    templet: function (d) {
                        //得到当前行数据，并拼接成自定义模板
                        if (d.type == 1) {
                            //return '可用'
                            return 'SAS正向计分'
                        } else if (d.type == 2) {
                            return 'SAS反向计分'
                        } else {
                            return '未知类型'
                        }

                    }
                }
                , {
                    field: 'status', title: '状态', width: 70,
                    templet: function (d) {
                        //得到当前行数据，并拼接成自定义模板
                        if (d.status == 0) {
                            //return '可用'
                            return '<a class="layui-btn  layui-btn-xs">可用</a>'
                        } else {
                            return '<a class="layui-btn layui-btn-disabled  layui-btn-xs">失效</a>'
                        }

                    }
                }
                , {field: 'createTime', title: '创建时间', width: 170}
                , {field: 'createBy', title: '创建人', width: 120}
                , {field: 'updateTime', title: '修改时间', width: 170}
                , {field: 'updateBy', title: '修改人', width: 120}
                , {fixed: 'right', width: 120, align: 'center', toolbar: '#rowBarDemo'}
            ]
            ]
        });
        //监听头工具栏事件
        table.on('toolbar(test)', function (obj) {
            var checkStatus = table.checkStatus(obj.config.id)
                , data = checkStatus.data; //获取选中的数据
            console.log(data)
            switch (obj.event) {
                case 'add':
                    $.ajax({
                        url: "${pageContext.request.contextPath}/questionSas/questionSasPage.do",
                        type: "POST",
                        dataType: "JSON",
                        success: function (ret) {
                            if (ret.code == 0) {
                                //重新渲染表单
                                layui.use('form', function () {
                                    var form = layui.form;
                                    form.render('select');
                                })
                            }
                        }
                    });
                    layer.open({
                        type: 1,
                        content: $('#saveQuestionForm')
                        //这里content是一个DOM，注意：最好该元素要存放在body最外层，否则可能被其它的相对元素所影响
                        , area: ['700px', '500px']
                    });
                    break;
                case 'del':
                    if (data.length === 0) {
                        layer.msg('请选择一行');
                    } else {
                        //定义一个数组，封装所有的id
                        var ids = [];
                        //循环所有选中的行
                        for (let i = 0; i < data.length; i++) {
                            ids[i] = data[i].id;
                        }
                        console.log(ids.join(","))
                        layer.confirm('真的删除行么', function (index) {
                            //去后台批量删除数据
                            $.ajax({
                                url: "${pageContext.request.contextPath}/questionSas/questionSasDel.do",
                                type: "POST",
                                dataType: "JSON",
                                data: {id: ids.join(",")},
                                success: function (data) {
                                    if (data.code == 0) {
                                        //关闭对话框
                                        layer.closeAll(); //疯狂模式，关闭所有层
                                        //刷新表格数据
                                        table.reload('demo', {
                                            page: {
                                                curr: 1 //重新从第 1 页开始
                                            }
                                        });

                                    }
                                    layer.msg(data.msg);
                                }

                            });
                        });
                    }
                    break;
            }
            ;
        });
        //监听行工具事件
        table.on('tool(test)', function (obj) { //注：tool 是工具条事件名，test 是 table 原始容器的属性 lay-filter="对应的值"
            var data = obj.data //获得当前行数据
                , layEvent = obj.event; //获得 lay-event 对应的值
            if (layEvent === 'update') {
                if (data.status == 0) {
                    document.getElementById("status").checked = true;
                } else {
                    document.getElementById("status").checked = false;
                }
                //updateFormFilter
                form.val("updateFormFilter", { //formTest 即 class="layui-form" 所在元素属性 lay-filter="" 对应的值
                    "id": data.id, // "name": "value"
                    "question": data.question,
                    "optionA": data.optionA,
                    "optionB": data.optionB,
                    "optionC": data.optionC,
                    "optionD": data.optionD,
                    "type": data.type,
                    "status": data.status
                });
                $.ajax({
                    url: "${pageContext.request.contextPath}/questionSas/questionSasUpdate.do",
                    type: "POST",
                    dataType: "JSON",
                    success: function (ret) {
                        if (ret.code == 0) {
                            var htmlStr = "";
                            for (let i = 0; i < ret.data.length; i++) {
                                if (data.type == ret.data[i].type) {
                                    htmlStr += "<option  value=" + ret.data[i].type + ">" + ret.data[i].type + "</option>"
                                } else {
                                    htmlStr += "<option value=" + ret.data[i].type + ">" + ret.data[i].type + "</option>"
                                }

                            }
                            console.log(htmlStr)
                            $("#updatetype").html(htmlStr);
                            //重新渲染表单
                            layui.use('form', function () {
                                var form = layui.form;
                                form.render('select');
                            })
                        }
                    }
                });
                layer.open({
                    type: 1,
                    content: $('#updateQuestionForm') //这里content是一个DOM，注意：最好该元素要存放在body最外层，否则可能被其它的相对元素所影响
                    , area: ['700px', '500px']
                });
            } else if (layEvent === 'del') {
                layer.confirm('真的删除行么', function (index) {
                    //去后台批量删除数据
                    $.ajax({
                        url: "${pageContext.request.contextPath}/questionSas/questionSasDel.do",
                        type: "POST",
                        dataType: "JSON",
                        data: {id: data.id},
                        success: function (data) {
                            if (data.code == 0) {
                                //关闭对话框
                                layer.closeAll(); //疯狂模式，关闭所有层
                                //刷新表格数据
                                table.reload('demo');

                            }
                            layer.msg(data.msg);
                        }

                    });
                });
            }
        });

        form.on('submit(updateFormFilter)', function (data) {
            if (data.field.status === '0') {
                data.field.status = 0;
            } else {
                data.field.status = 1;
            }
            console.log(data.elem) //被执行事件的元素DOM对象，一般为button对象
            console.log(data.form) //被执行提交的form对象，一般在存在form标签时才会返回
            console.log(data.field) //当前容器的全部表单字段，名值对形式：{name: value}
            $.ajax({
                url: "${pageContext.request.contextPath}/questionSas/questionSasUpdate.do",
                type: "POST",
                dataType: "JSON",
                data: data.field,
                success: function (ret) {
                    if (ret.code == 0) {
                        layer.closeAll();//关闭对话框
                        table.reload('demo', {
                            page: {
                                curr: 1 //重新从第 1 页开始
                            }
                        });
                    }
                    layer.msg(ret.msg);
                }
            });
            return false; //阻止表单跳转。如果需要表单跳转，去掉这段即可。
        });
        form.on('submit(saveQuestionFilter)', function (data) {
            console.log(data.elem) //被执行事件的元素DOM对象，一般为button对象
            console.log(data.form) //被执行提交的form对象，一般在存在form标签时才会返回
            console.log(data.field) //当前容器的全部表单字段，名值对形式：{name: value}
            $.ajax({
                url: "${pageContext.request.contextPath}/questionSas/saveQuestion.do",
                type: "POST",
                dataType: "JSON",
                data: data.field,
                success: function (ret) {
                    if (ret.code == 0) {
                        layer.closeAll();//关闭对话框
                        table.reload('demo', {
                            page: {
                                curr: 1 //重新从第 1 页开始
                            }
                        });
                    }
                    layer.msg(ret.msg);
                }
            });
            return false; //阻止表单跳转。如果需要表单跳转，去掉这段即可。
        });
        form.on('submit(selectFormfilter)', function (data) {
            console.log(data.elem) //被执行事件的元素DOM对象，一般为button对象
            console.log(data.form) //被执行提交的form对象，一般在存在form标签时才会返回
            console.log(data.field) //当前容器的全部表单字段，名值对形式：{name: value}
            table.reload('demo', {
                where: { //请求参数（注意：这里面的参数可任意定义，并非下面固定的格式）
                    question: data.field.question,//排序字段
                    createBy: data.field.createBy //排序字段
                }
                ,
                page: {
                    curr: 1
                }
            });
            return false; //阻止表单跳转。如果需要表单跳转，去掉这段即可。
        });
        $("#myButton").click(function () {
            table.reload('demo', {
                where: { //设定异步数据接口的额外参数，任意设
                    question: ""
                    , createBy: ""
                }
                , page: {
                    curr: 1 //重新从第 1 页开始
                }
            });
        })

    })
</script>
</html>
